package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqLoginBean;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.utils.EncryptUtils;

import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class LoginModel extends BaseModel {
    private boolean isLogin = false;

    public boolean login(@NonNull String username, @NonNull String pwd, @NonNull String code, @NonNull final LoginInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        String enPwd = EncryptUtils.getEncryptPassword(pwd);
        ReqLoginBean bean = new ReqLoginBean(username, enPwd, code);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.login(UrlHelper.LOGIN + UrlHelper.getUserName(),body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<UserBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(UserBean loginBean) {
                        isLogin = true;
                        infoHint.loginSuccessInfo(loginBean);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        isLogin = false;
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.loginFailInfo(e.message);
                    }

                });
        return isLogin;
    }


    public void getCode(@NonNull final ImageInfoHint infoHint) {
        httpService.getCodeImage().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                infoHint.getImageCodeSuccessInfo(response.body().byteStream());
                Log.e("下载图片", "成功" + response.body().byteStream());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("下载图片", "失败");
                ServerException e = ExceptionEngine.handleException(t);
                infoHint.getImageCodeFailInfo(e.toString());
            }
        });


    }


    //通过接口产生信息回调
    public interface LoginInfoHint {
        //登录
        void loginSuccessInfo(UserBean loginBean);

        void loginFailInfo(String str);


    }

    public interface ImageInfoHint {
        //图片验证码
        void getImageCodeSuccessInfo(InputStream inputStream);

        void getImageCodeFailInfo(String str);

    }
}
